//
//  InformationViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/2.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire
class InformationViewController: UIViewController {
    var  numstring:String!
    
    
     var InformationArray = [InformationModel]()
    
    
    
    var bigInformationArray = [InformationModel]()
    
    

    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
    
    var index = 1
    
    var tableView: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "信息公开"
        
        tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), style: .plain )
        tableView.dataSource = self
        tableView.delegate = self
        tableView.separatorInset = UIEdgeInsets.zero
        //        tableView.separatorStyle = .none
        tableView.backgroundColor = bgGrayColor
        tableView.showsHorizontalScrollIndicator = false
        tableView.showsVerticalScrollIndicator = false
        //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
        //tableView.tableHeaderView = UIView.init()
        tableView.tableFooterView = UIView.init()
        self.view.addSubview(tableView)

//       self.loadData()
        
        // 下拉刷新
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        // 现在的版本要用mj_header
        tableView.mj_header = header
        
        tableView.mj_header.beginRefreshing()
        
        // 上拉刷新
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
        tableView.mj_footer = footer
        
        
    }
    // 顶部刷新
    @objc func headerRefresh(){
        
        self.InformationArray.removeAll()
        self.bigInformationArray.removeAll()
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("信息公开=====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    let model1 =   InformationModel()
                    
                    model1.articlePic = dataDic["articlePic"].string
                    model1.createtime = dataDic["createtime"].string
                    
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    self.InformationArray.append(model1)
                    
                    
                    print("InformationArray====\(self.InformationArray.count)")
                    
                }
                
                self.bigInformationArray.append(contentsOf: self.InformationArray)
                
           
                self.tableView.reloadData()
                
                self.tableView.mj_header.endRefreshing()
                
                
            }else{
                self.tableView.mj_header.endRefreshing()
            }
            
        }
        
        
    }
    
    @objc func footerRefresh(){
        self.InformationArray.removeAll()
        
        index += 1
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(index)\",\"pageSize\":\"\(5)\"}"
        
        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("信息公开=====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    let model1 =   InformationModel()
                    
                    model1.articlePic = dataDic["articlePic"].string
                    model1.createtime = dataDic["createtime"].string
                    
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    self.InformationArray.append(model1)
                    
                    
                    print("InformationArray====\(self.InformationArray.count)")
                    
                }
                
                self.bigInformationArray.append(contentsOf: self.InformationArray)
                
                
                self.tableView.reloadData()
                
                self.tableView.mj_footer.endRefreshing()
                
                
            }else{
                self.tableView.mj_footer.endRefreshing()
            }
            
        }
        
    }
    func loadData(){
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(numstring!)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"

        print("json====\(json)")
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("信息公开=====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                for dataDic in  array! {
                    
                    let model1 =   InformationModel()

                    model1.articlePic = dataDic["articlePic"].string
                    model1.createtime = dataDic["createtime"].string

                    model1.title = dataDic["title"].string

                    model1.id = dataDic["id"].string

                    self.InformationArray.append(model1)


                    print("InformationArray====\(self.InformationArray.count)")
                    
                }
                
                self.tableView.reloadData()
                
                
            }else{
                
            }
            
        }
    }



}

extension InformationViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.bigInformationArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let Information:InformationModel = self.bigInformationArray[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = Information.title
        cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
        cell?.detailTextLabel?.text = Information.createtime
        cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 12)
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let Information:InformationModel = self.bigInformationArray[indexPath.row]
        
        let WebVC =  WebViewController()
        WebVC.numInteger = Information.id
        self.navigationController?.pushViewController(WebVC , animated: true)
    }
    
}
